import React, { Component } from 'react';
import { ImageBackground, FlatList, View, Image, RefreshControl, Text, TouchableOpacity, TextInput, StyleSheet } from 'react-native';
import { unitWidth, width } from '../constants/AdapterUtil';
import { MyHeader, MyRightImageHeader } from '../components/MyHeader';
import Colors from '../constants/Colors';
import MyListView from '../components/MyListView';
import ScrollableTabView, { ScrollableTabBar, DefaultTabBar } from 'react-native-scrollable-tab-view';
import NavigationService from '../constants/NavigationService';
import { postFetch } from '../constants/HttpExtension';
import { api_order_workorder_workorderList, api_order_workorder_employerOrderList, api_order_workorder_under_line_Confirm, api_order_workorder_deleteOrder, api_order_workorder_bidOrderList, api_order_workorder_isCooperation, api_order_workorder_confirmdelivery, api_order_get_list, api_order_op_finish, api_order_op_cancel, api_order_pay, api_order_op_delete, api_order_workorder_orderpay } from '../constants/HttpServices';
import { show } from '../constants/Toast';
import { DeleteTextView } from '../components/MyTextView';
import { SubmitButton9, SubmitButton10, SubmitButton7, SubmitButton12, SubmitButton11, ChooseButton, SubmitButton4, SubmitButton13, SubmitButton14, SubmitButton15, ChooseButton2 } from '../components/Button';
import CommonUtil from '../constants/CommonUtil';
import { Item2 } from '../components/Item';
import { CommonModal2, BottomPayModal, BottomReasonModal } from '../components/Modal';
import { wxPay } from '../constants/Pay';
import { contactBrowser } from '../constants/Gobrowser';

export default class MyOrderListScreen extends Component {

    constructor(props) {
        super(props)
        this.state = {
            user_type: global.userInfo.user_type,
        }
    }

    componentDidMount() {
        this.props.navigation.addListener('didFocus', payload => {
            if (this.refs.list1) {
                this.refs.list1.getData(1);
            }
            if (this.refs.list2) {
                this.refs.list2.getData(1);
            }
            if (this.refs.list3) {
                this.refs.list3.getData(1);
            }
        })
    }

    render() {

        return (
            <View style={{ flex: 1, backgroundColor: Colors.colorF5 }}>
                <MyHeader title='订单中心' leftImg={require('../res/ic_arrow_left.png')} leftClick={() => { this.props.navigation.pop() }} />
                <ScrollableTabView
                    style={{ backgroundColor: Colors.white }}
                    tabBarUnderlineStyle={{ backgroundColor: Colors.transparent }}
                    tabBarActiveTextColor={Colors.colorOrange}
                    tabBarInactiveTextColor={Colors.color99}
                    initialPage={0}
                    onChangeTab={(value) => {

                    }}
                    renderTabBar={() => <DefaultTabBar style={{
                        height: 50,
                        borderColor: Colors.transparent,
                    }} renderTab={
                        (name, page, isTabActive, onPressHandler, onLayoutHandler) => {
                            const { activeTextColor, inactiveTextColor, textStyle, } = this.props;
                            const textColor = isTabActive ? Colors.colorOrange : inactiveTextColor;
                            const fontWeight = isTabActive ? 'bold' : 'normal';
                            const fontSize = isTabActive ? 15 : 15;

                            return <TouchableOpacity
                                style={{ flex: 1, }}
                                key={name}
                                onPress={() => onPressHandler(page)}>
                                <View style={{
                                    flex: 1,
                                    alignItems: 'center',
                                    justifyContent: 'center',
                                    paddingBottom: 10,
                                }}>
                                    <Text style={[{ color: textColor, fontWeight, fontSize: fontSize }, textStyle,]}>
                                        {name}
                                    </Text>
                                    {isTabActive ?
                                        <View style={{ width: 60 * unitWidth, height: 3 * unitWidth, backgroundColor: Colors.colorOrange, borderRadius: 15, position: 'absolute', bottom: 0 }} /> : null}
                                </View>
                            </TouchableOpacity>;
                        }} />}>
                    <GoodsList ref='list1' tabLabel='商品订单' />
                    <NeedsList ref='list2' tabLabel='需求订单' />
                    {this.state.user_type == '2' || this.state.user_type == '3' ?
                        <BidList ref='list3' tabLabel='投标订单' />
                        : null}
                </ScrollableTabView>
            </View>
        );
    }
}

class GoodsList extends Component {

    constructor(props) {
        super(props)
        this.state = {
            list: [],

            currentChoose: 0
        }
    }

    componentDidMount() {
        this.refs.MyListView.onRefresh();
    }

    render() {

        return (
            <View style={{ flex: 1, backgroundColor: Colors.colorF5 }}>
                <View style={{
                    height: 44 * unitWidth, flexDirection: 'row', backgroundColor: Colors.white,
                    alignItems: 'center', justifyContent: 'center', paddingHorizontal: 10 * unitWidth, marginTop: 0.5
                }}>
                    <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
                        <ChooseButton2 text='全部' currentChoose={this.state.currentChoose} index={0} onPress={() => {
                            this.setState({
                                currentChoose: 0,
                                list: []
                            }, () => {
                                this.refs.MyListView.onRefresh();
                            })
                        }} />
                    </View>
                    <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
                        <ChooseButton2 text='待付款' currentChoose={this.state.currentChoose} index={1} onPress={() => {
                            this.setState({
                                currentChoose: 1,
                                list: []
                            }, () => {
                                this.refs.MyListView.onRefresh();
                            })
                        }} />
                    </View>
                    <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
                        <ChooseButton2 text='待发货' currentChoose={this.state.currentChoose} index={2} onPress={() => {
                            this.setState({
                                currentChoose: 2,
                                list: []
                            }, () => {
                                this.refs.MyListView.onRefresh();
                            })
                        }} />
                    </View>
                    <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
                        <ChooseButton2 text='待收货' currentChoose={this.state.currentChoose} index={3} onPress={() => {
                            this.setState({
                                currentChoose: 3,
                                list: []
                            }, () => {
                                this.refs.MyListView.onRefresh();
                            })
                        }} />
                    </View>
                    <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
                        <ChooseButton2 text='已完成' currentChoose={this.state.currentChoose} index={4} onPress={() => {
                            this.setState({
                                currentChoose: 4,
                                list: []
                            }, () => {
                                this.refs.MyListView.onRefresh();
                            })
                        }} />
                    </View>
                </View>
                <MyListView
                    ref='MyListView'
                    list={this.state.list}
                    renderItem={this.getItem.bind(this)}
                    getList={this.getData.bind(this)}
                    canLoadMore={true}
                />
                <CommonModal2 ref='CommonModal2' title='是否确认删除该订单？' comfirmText='确定' cancelText='取消'
                    cancelClick={() => {

                    }}
                    comfirmClick={() => {
                        this.deleteOrder();
                    }} />
                <BottomPayModal ref='BottomPayModal' onClick={(index) => {
                    this.payOrder(index)
                }} />
                <BottomReasonModal ref='BottomReasonModal' onClick={(index) => {
                    this.cancelOrder();
                }} />
            </View>
        );
    }

    getItem({ item, index }) {
        return (
            <View style={{ alignItems: 'center' }}>
                <TouchableOpacity style={{
                    width: width, minHeight: 150 * unitWidth, borderRadius: 7,
                    backgroundColor: Colors.white, marginTop: 3 * unitWidth, paddingHorizontal: 15 * unitWidth
                }} activeOpacity={0.7} onPress={() => { NavigationService.navigate('GoodsOrderDetail', { id: item.id, isChild: item.is_child_member }) }}>
                    <FlatList
                        style={{ alignSelf: 'stretch', flex: 1 }}
                        keyExtractor={(item, index) => {
                            return index.toString()
                        }}
                        ListHeaderComponent={
                            <View>
                                <View style={{ height: 40 * unitWidth, flexDirection: 'row', alignItems: 'center' }}>
                                    <Text style={{ fontSize: 14, color: Colors.black, marginLeft: 10 * unitWidth }}>{item.ordersn}</Text>
                                    <Text style={{ fontSize: 12, color: Colors.colorOrange, position: 'absolute', right: 5 * unitWidth }}>{item.statusstr == '待评价' ? '已完成' : item.statusstr}</Text>
                                </View>
                                <View style={{ backgroundColor: Colors.colorEF, width: 350 * unitWidth, alignSelf: 'center', height: 0.5, }} />
                            </View>
                        }
                        showsVerticalScrollIndicator={false}
                        data={item.goods}
                        renderItem={this.getItem2.bind(this)}
                    />
                    <View style={{ backgroundColor: Colors.colorEF, width: width, alignSelf: 'center', height: 0.5 }} />
                    <View style={{ height: 44 * unitWidth, flexDirection: 'row', alignItems: 'center', justifyContent: 'center', paddingHorizontal: 10 * unitWidth }}>
                        <View style={{ flex: 1, alignItems: 'flex-start', justifyContent: 'center' }}>
                            <Text style={{ fontSize: 15, color: Colors.black }}>共{item.total}件 , 合计:<Text style={{ color: Colors.colorRed }}>￥{item.price}</Text></Text>
                        </View>
                        <View style={{ flex: 1, alignItems: 'center', justifyContent: 'flex-end', flexDirection: 'row' }}>
                            {this.getStatusBtn(item)}
                        </View>
                    </View>
                </TouchableOpacity>
            </View>
        )
    }

    getItem2({ item, index }) {
        return (
            <View style={{ alignItems: 'center' }}>
                <View style={{
                    width: width, minHeight: 150 * unitWidth, borderRadius: 7,
                    backgroundColor: Colors.white, marginTop: 3 * unitWidth, paddingHorizontal: 15 * unitWidth
                }}>
                    <View style={{ height: 40 * unitWidth, flexDirection: 'row', alignItems: 'center' }}>
                        <Image style={{
                            width: 12 * unitWidth, height: 11 * unitWidth, marginLeft: 10 * unitWidth
                        }}
                            source={require('../res/gwc_gs.png')} resizeMode='stretch' />
                        <Text style={{ fontSize: 14, color: Colors.black, marginLeft: 5 * unitWidth }}>{item.shopname}</Text>
                        <Image style={{
                            width: 5 * unitWidth, height: 10 * unitWidth, marginLeft: 10 * unitWidth
                        }}
                            source={require('../res/me_back.png')} resizeMode='stretch' />
                    </View>
                    <View style={{ backgroundColor: Colors.colorEF, width: 350 * unitWidth, alignSelf: 'center', height: 0.5, }} />
                    <View>
                        {this.getGoodsView(item.goods, index)}
                    </View>
                </View>
            </View>
        )
    }

    getGoodsView(list, index) {
        let views = []
        for (let i = 0, len = list.length; i < len; i++) {
            let item = list[i];
            views.push(
                <View style={{ height: 109 * unitWidth, flexDirection: 'row', alignItems: 'center' }}>
                    <Image style={{
                        width: 110 * unitWidth, height: 80 * unitWidth, marginLeft: 9 * unitWidth
                    }}
                        source={{ uri: item.thumb }} resizeMode='stretch' />
                    <View style={{ marginLeft: 10 * unitWidth }}>
                        <Text style={{ color: Colors.black, fontSize: 14, marginVertical: 4 * unitWidth, width: 190 * unitWidth }}>{item.title}</Text>
                        <View style={{
                            paddingHorizontal: 7 * unitWidth, borderWidth: 0.5, marginTop: 5 * unitWidth,
                            borderColor: Colors.colorRed, borderRadius: 5, alignSelf: 'flex-start'
                        }}>
                            <Text style={{ color: Colors.colorRed, fontSize: 10, }}>线下价格:￥{item.offlineprice}</Text>
                        </View>
                        <View style={{ alignSelf: 'flex-start', marginTop: 2 * unitWidth, flexDirection: 'row', alignItems: 'center', width: 210 * unitWidth }}>
                            <Text style={{ color: Colors.colorRed, fontSize: 17, }}>￥{item.marketprice}</Text>
                            <DeleteTextView text={'￥' + item.productprice} style={{ marginLeft: 3 * unitWidth }} />
                            <Text style={{ color: Colors.black, fontSize: 17, position: 'absolute', right: 0, }}>x{item.total}</Text>
                        </View>
                    </View>
                </View>
            );
        }
        return views
    }

    getStatusBtn(item) {
        if (item.status == '0') {
            if (global.userInfo.user_type == '3') {
                return null;
            } else {
                return (
                    <View style={{ flexDirection: 'row', alignItems: 'center' }}>
                        <SubmitButton10 style={{ marginRight: 10 * unitWidth }} text='取消订单' onPress={() => {
                            this.id = item.id
                            this.refs.BottomReasonModal.showModal();
                        }} />
                        <SubmitButton12 text='付款' onPress={() => {
                            this.id = item.id
                            this.refs.BottomPayModal.showModal();
                        }} />
                    </View>
                )
            }
        } else if (item.status == '1') {
            if (item.is_child_member == '1' && global.userInfo.user_type != '3') {
                return null;
            }
            return (
                <View style={{ flexDirection: 'row', alignItems: 'center' }}>
                    <SubmitButton11 style={{}} text='待发货' />
                </View>
            )
        } else if (item.status == '2') {
            if (item.is_child_member == '1' && global.userInfo.user_type != '3') {
                return null;
            }
            return (
                <View style={{ flexDirection: 'row', alignItems: 'center' }}>
                    <SubmitButton12 style={{}} text='确认收货' onPress={() => {
                        this.id = item.id
                        this.finishOrder();
                    }} />
                </View>
            )
        } else if (item.status == '3' || item.status == '-1') {
            if (item.is_child_member == '1' && global.userInfo.user_type != '3') {
                return null;
            }
            return (
                <View style={{ flexDirection: 'row', alignItems: 'center' }}>
                    <SubmitButton10 style={{}} text='删除订单' onPress={() => {
                        this.id = item.id
                        this.refs.CommonModal2.showModal();
                    }} />
                </View>
            )
        }
    }

    //获取工单信息
    getData(page) {
        let params = {
            page: page,
        }
        if (this.state.currentChoose != 0) {
            params['status'] = this.state.currentChoose - 1
        }
        postFetch(api_order_get_list, params, {
            success: (response) => {
                if (page == 1) {
                    this.setState({
                        list: response.result.list,
                    }, () => {
                        this.refs.MyListView.onHasMore();
                    })
                } else {
                    if (response.result.list && response.result.list.length > 0) {
                        this.setState({
                            list: this.state.list.concat(response.result.list),
                        }, () => {
                            this.refs.MyListView.onHasMore();
                        })
                    } else {
                        this.refs.MyListView.onNoMore();
                    }
                }
            },
            fail: (response) => {
                show(response.result.message);
            }
        })
    }

    //支付订单
    payOrder(paytype) {
        let params = {
            id: this.id,
        }
        if (paytype == 1) {
            params['paytype'] = 'wechat'
        } else if (paytype == 2) {
            params['paytype'] = 'alipay'
        } else if (paytype == 3) {
            params['paytype'] = 'credit'
        }
        postFetch(api_order_pay, params, {
            success: (response) => {
                if (paytype == 1) {
                    wxPay(response.result.wechat, {
                        success: () => {
                            show('支付成功');
                            this.refs.MyListView.onHasMore();
                        },
                        fail: () => {

                        }
                    })
                } else if (paytype == 2) {
                    contactBrowser(response.result.alipay.url)
                } else if (paytype == 3) {
                    show('支付成功');
                    this.refs.MyListView.onHasMore();
                }
            },
            fail: (response) => {
                show(response.result.message);
            }
        })
    }

    //取消订单
    cancelOrder() {
        let params = {
            id: this.id,
        }
        postFetch(api_order_op_cancel, params, {
            success: (response) => {
                show('取消成功');
                this.refs.MyListView.onRefresh();
            },
            fail: (response) => {
                show(response.result.message);
            }
        })
    }

    //完成订单
    finishOrder() {
        let params = {
            id: this.id,
        }
        postFetch(api_order_op_finish, params, {
            success: (response) => {
                show('确认成功');
                this.refs.MyListView.onRefresh();
            },
            fail: (response) => {
                show(response.result.message);
            }
        })
    }

    //删除订单
    deleteOrder() {
        let params = {
            id: this.id,
            userdeleted: 1
        }
        postFetch(api_order_op_delete, params, {
            success: (response) => {
                show('删除成功');
                this.refs.MyListView.onRefresh();
            },
            fail: (response) => {
                show(response.result.message);
            }
        })
    }
}

class NeedsList extends Component {

    constructor(props) {
        super(props)
        this.state = {
            list: [],

            currentChoose: 0,

        }
        this.chooseNo = 0
    }

    componentDidMount() {
        this.refs.MyListView.onRefresh();
    }

    render() {

        return (
            <View style={{ flex: 1, backgroundColor: Colors.colorF5 }}>
                <View style={{
                    height: 44 * unitWidth, flexDirection: 'row', backgroundColor: Colors.white,
                    alignItems: 'center', justifyContent: 'center', paddingHorizontal: 10 * unitWidth, marginTop: 0.5
                }}>
                    <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
                        <ChooseButton text='全部' currentChoose={this.state.currentChoose} index={0} onPress={() => {
                            this.setState({
                                currentChoose: 0,
                                list: []
                            }, () => {
                                this.refs.MyListView.onRefresh();
                            })
                        }} />
                    </View>
                    <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
                        <ChooseButton text='进行中' currentChoose={this.state.currentChoose} index={1} onPress={() => {
                            this.setState({
                                currentChoose: 1,
                                list: []
                            }, () => {
                                this.refs.MyListView.onRefresh();
                            })
                        }} />
                    </View>
                    <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
                        <ChooseButton text='已完成' currentChoose={this.state.currentChoose} index={2} onPress={() => {
                            this.setState({
                                currentChoose: 2,
                                list: []
                            }, () => {
                                this.refs.MyListView.onRefresh();
                            })
                        }} />
                    </View>
                    <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
                        <ChooseButton text='已失败' currentChoose={this.state.currentChoose} index={3} onPress={() => {
                            this.setState({
                                currentChoose: 3,
                                list: []
                            }, () => {
                                this.refs.MyListView.onRefresh();
                            })
                        }} />
                    </View>
                </View>
                <MyListView
                    ref='MyListView'
                    list={this.state.list}
                    renderItem={this.getItem.bind(this)}
                    getList={this.getData.bind(this)}
                    canLoadMore={true}
                />
                <CommonModal2 ref='CommonModal1' title='是否删除订单？' comfirmText='确定' cancelText='取消'
                    cancelClick={() => {

                    }}
                    comfirmClick={() => {
                        this.deleteOrder(this.chooseNo);
                    }} />

                <CommonModal2 ref='CommonModal2' title='是否确认收货？' comfirmText='确定' cancelText='取消'
                    cancelClick={() => {

                    }}
                    comfirmClick={() => {
                        this.confirmReceive(this.chooseNo);
                    }} />
                <BottomPayModal ref='BottomPayModal' onClick={(index) => {
                    this.pay(index);
                }} />
            </View>
        );
    }

    getItem({ item, index }) {
        return (
            <View style={{ alignItems: 'center' }}>
                <TouchableOpacity style={{
                    width: width, minHeight: 150 * unitWidth, borderRadius: 7,
                    backgroundColor: Colors.white, marginTop: 3 * unitWidth, paddingHorizontal: 15 * unitWidth
                }} activeOpacity={0.7} onPress={() => { NavigationService.navigate('NeedsOrderDetail', { no: item.out_trade_no }) }}>
                    <View style={{ height: 40 * unitWidth, flexDirection: 'row', alignItems: 'center' }}>
                        <Text style={{ fontSize: 14, color: Colors.black, marginLeft: 10 * unitWidth }}>订单编号：{item.out_trade_no}</Text>
                        <Item2 text={item.pay_type == '2' ? '线下' : '线上'} style={{ marginLeft: 10 * unitWidth }} />
                        {this.getStatusText(item.order_status)}
                    </View>
                    <View style={{ backgroundColor: Colors.colorEF, width: 350 * unitWidth, alignSelf: 'center', height: 0.5, }} />
                    <View style={{ height: 109 * unitWidth, flexDirection: 'row', alignItems: 'center' }}>
                        <Image style={{
                            width: 110 * unitWidth, height: 90 * unitWidth, marginLeft: 9 * unitWidth
                        }}
                            source={{ uri: item.picture }} resizeMode='stretch' />
                        <View style={{ marginLeft: 10 * unitWidth }}>
                            <Text style={{ color: Colors.black, fontSize: 14, width: 190 * unitWidth }}>{item.title}</Text>
                            <Text style={{ fontSize: 13, color: Colors.color99, marginTop: 5 * unitWidth }}>加工数目：{item.total_count}</Text>
                            <Text style={{ fontSize: 13, color: Colors.black, marginTop: 5 * unitWidth }}>* 中标价格：<Text style={{ color: Colors.colorRed }}>￥{item.total_price}</Text></Text>
                            <Text style={{ fontSize: 13, color: Colors.color99, marginTop: 5 * unitWidth }}>* 服务商：{item.merchname}</Text>
                        </View>
                    </View>
                    <View style={{ height: 44 * unitWidth, flexDirection: 'row', alignItems: 'center', justifyContent: 'center', paddingHorizontal: 10 * unitWidth }}>
                        <View style={{ flex: 1, alignItems: 'flex-start', justifyContent: 'center' }}>
                            <Text style={{ fontSize: 13, color: Colors.color99 }}>{item.createtime}</Text>
                        </View>
                        <View style={{ flex: 1, alignItems: 'center', justifyContent: 'flex-end', flexDirection: 'row' }}>
                            {this.getStatusBtn(item)}
                        </View>
                    </View>
                </TouchableOpacity>
            </View>
        )
    }

    getStatusBtn(item) {
        if (item.is_cooperation == '0') {
            return (
                <View style={{ flexDirection: 'row', alignItems: 'center' }}>
                    <SubmitButton11 style={{}} text='待服务商确认' />
                </View>
            )
        } else if (item.is_cooperation == '1' && item.is_contract == '0') {
            return (
                <View style={{ flexDirection: 'row', alignItems: 'center' }}>
                    <SubmitButton15 style={{}} text='去签署电子合同'
                        onPress={() => {
                            NavigationService.navigate('SigningContract', { no: item.out_trade_no })
                        }} />
                </View>
            )
        } else if (item.is_cooperation == '2') {
            return (
                <View style={{ flexDirection: 'row', alignItems: 'center' }}>
                    <SubmitButton14 style={{}} text='服务商拒绝合作' />
                </View>
            )
        } else if (item.status == '0') {
            return (
                <View style={{ flexDirection: 'row', alignItems: 'center' }}>
                    <SubmitButton12 style={{}} text='去支付'
                        onPress={() => {
                            NavigationService.navigate('NeedsOrderDetail', { no: item.out_trade_no })
                        }} />
                </View>
            )
        } else if (item.status == '1') {
            return (
                <View style={{ flexDirection: 'row', alignItems: 'center' }}>
                    <SubmitButton11 style={{}} text='待服务商交货' />
                </View>
            )
        } else if (item.status == '3') {
            return (
                <View style={{ flexDirection: 'row', alignItems: 'center' }}>
                    <SubmitButton12 style={{}} text='确认收货' onPress={() => {
                        this.chooseNo = item.out_trade_no
                        this.refs.CommonModal2.showModal();
                    }} />
                </View>
            )
        } else if (item.status == '2') {
            if (item.is_employer_comment == '0') {
                return (
                    <View style={{ flexDirection: 'row', alignItems: 'center' }}>
                        <SubmitButton12 style={{}} text='评价' onPress={() => { NavigationService.navigate('EvaluateService', { no: item.out_trade_no }) }} />
                    </View>
                )
            } else {
                return (
                    <View style={{ flexDirection: 'row', alignItems: 'center' }}>
                        <SubmitButton10 style={{}} text='删除订单' onPress={() => {
                            this.chooseNo = item.out_trade_no
                            this.refs.CommonModal1.showModal();
                        }} />
                    </View>
                )
            }
        } else if (item.status == '4') {
            return (
                <View style={{ flexDirection: 'row', alignItems: 'center' }}>
                    <SubmitButton10 style={{}} text='删除订单' onPress={() => {
                        this.chooseNo = item.out_trade_no
                        this.refs.CommonModal1.showModal();
                    }} />
                </View>
            )
        } else if (item.status == '5') {
            return (
                <View style={{ flexDirection: 'row', alignItems: 'center' }}>
                    <SubmitButton14 style={{}} text='超出规定时间' />
                </View>
            )
        }
    }

    getStatusText(type) {
        if (type == '1') {
            return (
                <Text style={{ fontSize: 12, color: Colors.colorOrange, position: 'absolute', right: 5 * unitWidth }}>已完成</Text>
            )
        } else if (type == '2') {
            return (
                <Text style={{ fontSize: 12, color: Colors.colorOrange, position: 'absolute', right: 5 * unitWidth }}>交易中</Text>
            )
        } else if (type == '3') {
            return (
                <Text style={{ fontSize: 12, color: Colors.color99, position: 'absolute', right: 5 * unitWidth }}>交易失败</Text>
            )
        }
    }

    //获取工单信息
    getData(page) {
        let params = {
            page: page,
            status: this.state.currentChoose
        }
        if (this.state.currentChoose == 1) {
            params.status = 2
        } else if (this.state.currentChoose == 2) {
            params.status = 1
        }
        postFetch(api_order_workorder_employerOrderList, params, {
            success: (response) => {
                if (page == 1) {
                    this.setState({
                        list: response.result.list,
                    }, () => {
                        this.refs.MyListView.onHasMore();
                    })
                } else {
                    if (response.result.list && response.result.list.length > 0) {
                        this.setState({
                            list: this.state.list.concat(response.result.list),
                        }, () => {
                            this.refs.MyListView.onHasMore();
                        })
                    } else {
                        this.refs.MyListView.onNoMore();
                    }
                }
            },
            fail: (response) => {
                show(response.result.message);
            }
        })
    }

    deleteOrder(no) {
        let params = {
            out_trade_no: no
        }
        postFetch(api_order_workorder_deleteOrder, params, {
            success: (response) => {
                show('删除成功');
                this.refs.MyListView.onRefresh();
            },
            fail: (response) => {
                show(response.result.message);
            }
        })
    }

    //确认收货
    confirmReceive(no) {
        let params = {
            out_trade_no: no
        }
        postFetch(api_order_workorder_under_line_Confirm, params, {
            success: (response) => {
                if (response.result.status == '1') {
                    this.refs.BottomPayModal.showModal();
                } else {
                    show('确认成功');
                    this.refs.MyListView.onRefresh();
                }
            },
            fail: (response) => {
                show(response.result.message);
            }
        })
    }

    pay(type) {
        let params = {
            'out_trade_no': this.chooseNo,
            'payment_type': type,
        }
        postFetch(api_order_workorder_orderpay, params, {
            success: (response) => {
                if (type == 1) {
                    wxPay(response.result, {
                        success: () => {
                            show('支付成功');
                            this.props.navigation.pop();
                        },
                        fail: () => {

                        }
                    })
                } else if (type == 2) {
                    contactBrowser(response.result.url)
                } else if (type == 3) {
                    show('支付成功');
                    this.refs.MyListView.onRefresh();
                }
            },
            fail: (response) => {
                show(response.result.message);
            }
        })
    }
}

class BidList extends Component {

    constructor(props) {
        super(props)
        this.state = {
            list: [],

            currentChoose: 0,
        }
        this.chooseNo = 0
    }

    componentDidMount() {
        this.refs.MyListView.onRefresh();
    }

    render() {
        return (
            <View style={{ flex: 1, backgroundColor: Colors.colorF5 }}>
                <View style={{
                    height: 44 * unitWidth, flexDirection: 'row', backgroundColor: Colors.white,
                    alignItems: 'center', justifyContent: 'center', paddingHorizontal: 10 * unitWidth, marginTop: 0.5
                }}>
                    <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
                        <ChooseButton text='全部' currentChoose={this.state.currentChoose} index={0} onPress={() => {
                            this.setState({
                                currentChoose: 0,
                                list: []
                            }, () => {
                                this.refs.MyListView.onRefresh();
                            })
                        }} />
                    </View>
                    <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
                        <ChooseButton text='进行中' currentChoose={this.state.currentChoose} index={1} onPress={() => {
                            this.setState({
                                currentChoose: 1,
                                list: []
                            }, () => {
                                this.refs.MyListView.onRefresh();
                            })
                        }} />
                    </View>
                    <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
                        <ChooseButton text='已完成' currentChoose={this.state.currentChoose} index={2} onPress={() => {
                            this.setState({
                                currentChoose: 2,
                                list: []
                            }, () => {
                                this.refs.MyListView.onRefresh();
                            })
                        }} />
                    </View>
                    <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
                        <ChooseButton text='已失败' currentChoose={this.state.currentChoose} index={3} onPress={() => {
                            this.setState({
                                currentChoose: 3,
                                list: []
                            }, () => {
                                this.refs.MyListView.onRefresh();
                            })
                        }} />
                    </View>
                </View>
                <MyListView
                    ref='MyListView'
                    list={this.state.list}
                    renderItem={this.getItem.bind(this)}
                    getList={this.getData.bind(this)}
                    canLoadMore={true}
                />
                <CommonModal2 ref='CommonModal1' title='是否删除订单？' comfirmText='确定' cancelText='取消'
                    cancelClick={() => {

                    }}
                    comfirmClick={() => {
                        this.deleteOrder(this.chooseNo);
                    }} />

                <CommonModal2 ref='CommonModal2' title='是否确认送货？' comfirmText='确定' cancelText='取消'
                    cancelClick={() => {

                    }}
                    comfirmClick={() => {
                        this.confirmSend(this.chooseNo);
                    }} />

                <CommonModal2 ref='CommonModal3' title='是否确认合作？' comfirmText='确定' cancelText='取消'
                    cancelClick={() => {

                    }}
                    comfirmClick={() => {
                        this.confirmCooperation(this.chooseNo);
                    }} />
            </View>
        );
    }

    getItem({ item, index }) {
        return (
            <View style={{ alignItems: 'center' }}>
                <TouchableOpacity style={{
                    width: width, minHeight: 150 * unitWidth, borderRadius: 7,
                    backgroundColor: Colors.white, marginTop: 3 * unitWidth, paddingHorizontal: 15 * unitWidth
                }} activeOpacity={0.7} onPress={() => { NavigationService.navigate('BidOrderDetail', { no: item.out_trade_no }) }}>
                    <View style={{ height: 40 * unitWidth, flexDirection: 'row', alignItems: 'center' }}>
                        <Text style={{ fontSize: 14, color: Colors.black, marginLeft: 10 * unitWidth }}>订单编号：{item.out_trade_no}</Text>
                        <Item2 text={item.pay_type == '2' ? '线下' : '线上'} style={{ marginLeft: 10 * unitWidth }} />
                        {this.getStatusText(item.order_status)}
                    </View>
                    <View style={{ backgroundColor: Colors.colorEF, width: 350 * unitWidth, alignSelf: 'center', height: 0.5, }} />
                    <View style={{ height: 109 * unitWidth, flexDirection: 'row', alignItems: 'center' }}>
                        <Image style={{
                            width: 110 * unitWidth, height: 90 * unitWidth, marginLeft: 9 * unitWidth
                        }}
                            source={{ uri: item.picture }} resizeMode='stretch' />
                        <View style={{ marginLeft: 10 * unitWidth }}>
                            <Text style={{ color: Colors.black, fontSize: 14, width: 190 * unitWidth }}>{item.title}</Text>
                            <Text style={{ fontSize: 13, color: Colors.color99, marginTop: 5 * unitWidth }}>加工数目：{item.total_count}</Text>
                            <Text style={{ fontSize: 13, color: Colors.black, marginTop: 5 * unitWidth }}>* 中标价格：<Text style={{ color: Colors.colorRed }}>￥{item.total_price}</Text></Text>
                            <Text style={{ fontSize: 13, color: Colors.color99, marginTop: 5 * unitWidth }}>* 雇主：{item.merchname}</Text>
                        </View>
                    </View>
                    <View style={{ height: 44 * unitWidth, flexDirection: 'row', alignItems: 'center', justifyContent: 'center', paddingHorizontal: 10 * unitWidth }}>
                        <View style={{ flex: 1, alignItems: 'flex-start', justifyContent: 'center' }}>
                            <Text style={{ fontSize: 13, color: Colors.color99 }}>{item.createtime}</Text>
                        </View>
                        <View style={{ flex: 1, alignItems: 'center', justifyContent: 'flex-end', flexDirection: 'row' }}>
                            {this.getStatusBtn(item)}
                        </View>
                    </View>
                </TouchableOpacity>
            </View>
        )
    }

    getStatusBtn(item) {
        if (item.is_cooperation == '0') {
            return (
                <View style={{ flexDirection: 'row', alignItems: 'center' }}>
                    <SubmitButton7 text='确认合作' onPress={() => {
                        this.chooseNo = item.out_trade_no
                        this.refs.CommonModal3.showModal()
                    }} />
                </View>
            )
        } else if (item.is_cooperation == '1' && item.is_contract == '0') {
            return (
                <View style={{ flexDirection: 'row', alignItems: 'center' }}>
                    <SubmitButton11 style={{}} text='待签署合同'
                        onPress={() => {
                            // this.props.navigation.navigate('SigningContract', { id: this.state.info.id })
                        }} />
                </View>
            )
        } else if (item.status == '0') {
            return (
                <View style={{ flexDirection: 'row', alignItems: 'center' }}>
                    <SubmitButton11 style={{}} text='待雇主支付'
                        onPress={() => {

                        }} />
                </View>
            )
        } else if (item.is_cooperation == '2') {
            return (
                <View style={{ flexDirection: 'row', alignItems: 'center' }}>
                    <SubmitButton14 style={{}} text='服务商拒绝合作' />
                </View>
            )
        } else if (item.status == '1') {
            return (
                <View style={{ flexDirection: 'row', alignItems: 'center' }}>
                    <SubmitButton7 text='确认送货' onPress={() => {
                        this.chooseNo = item.out_trade_no
                        this.refs.CommonModal2.showModal()
                    }} />
                </View>
            )
        } else if (item.status == '3') {
            return (
                <View style={{ flexDirection: 'row', alignItems: 'center' }}>
                    <SubmitButton11 style={{}} text='待雇主确认收货'
                        onPress={() => {

                        }} />
                </View>
            )
        } else if (item.status == '2') {
            if (item.is_service_comment == '0') {
                return (//线上评价
                    <View style={{ flexDirection: 'row', alignItems: 'center' }}>
                        <SubmitButton12 style={{}} text='评价' onPress={() => { NavigationService.navigate('EvaluateEmployer', { no: item.out_trade_no }) }} />
                    </View>
                )
            } else {
                return (
                    <View style={{ flexDirection: 'row', alignItems: 'center' }}>
                        <SubmitButton10 style={{}} text='删除订单' onPress={() => {
                            this.chooseNo = item.out_trade_no
                            this.refs.CommonModal1.showModal();
                        }} />
                    </View>
                )
            }
        } else if (item.status == '4') {
            return (
                <View style={{ flexDirection: 'row', alignItems: 'center' }}>
                    <SubmitButton10 style={{}} text='删除订单' onPress={() => {
                        this.chooseNo = item.out_trade_no
                        this.refs.CommonModal1.showModal();
                    }} />
                </View>
            )
        } else if (item.status == '5') {
            return (
                <View style={{ flexDirection: 'row', alignItems: 'center' }}>
                    <SubmitButton14 style={{}} text='超出规定时间' />
                </View>
            )
        }
    }

    getStatusText(type) {
        if (type == '2') {
            return (
                <Text style={{ fontSize: 12, color: Colors.colorOrange, position: 'absolute', right: 5 * unitWidth }}>交易中</Text>
            )
        } else if (type == '1') {
            return (
                <Text style={{ fontSize: 12, color: Colors.colorOrange, position: 'absolute', right: 5 * unitWidth }}>已完成</Text>
            )
        } else if (type == '3') {
            return (
                <Text style={{ fontSize: 12, color: Colors.color99, position: 'absolute', right: 5 * unitWidth }}>交易失败</Text>
            )
        }
    }

    //获取工单信息
    getData(page) {
        let params = {
            page: page,
            status: this.state.currentChoose
        }
        if (this.state.currentChoose == 1) {
            params.status = 2
        } else if (this.state.currentChoose == 2) {
            params.status = 1
        }
        postFetch(api_order_workorder_bidOrderList, params, {
            success: (response) => {
                if (page == 1) {
                    this.setState({
                        list: response.result.list,
                    }, () => {
                        this.refs.MyListView.onHasMore();
                    })
                } else {
                    if (response.result.list && response.result.list.length > 0) {
                        this.setState({
                            list: this.state.list.concat(response.result.list),
                        }, () => {
                            this.refs.MyListView.onHasMore();
                        })
                    } else {
                        this.refs.MyListView.onNoMore();
                    }
                }
            },
            fail: (response) => {
                show(response.result.message);
            }
        })
    }

    deleteOrder(no) {
        let params = {
            out_trade_no: no
        }
        postFetch(api_order_workorder_deleteOrder, params, {
            success: (response) => {
                show('删除成功');
                this.refs.MyListView.onRefresh();
            },
            fail: (response) => {
                show(response.result.message);
            }
        })
    }

    //确认交货
    confirmSend(no) {
        let params = {
            out_trade_no: no
        }
        postFetch(api_order_workorder_confirmdelivery, params, {
            success: (response) => {
                show('确认成功');
                this.refs.MyListView.onRefresh();
            },
            fail: (response) => {
                show(response.result.message);
            }
        })
    }

    //确认合作
    confirmCooperation(no) {
        let params = {
            out_trade_no: no,
            status: 1
        }
        postFetch(api_order_workorder_isCooperation, params, {
            success: (response) => {
                show('确认成功');
                this.refs.MyListView.onRefresh();
            },
            fail: (response) => {
                show(response.result.message);
            }
        })
    }
}